package cn.lixin.user.controller;

import cn.lixin.basic.exception.BusinessException;
import cn.lixin.basic.utils.AjaxResult;
import cn.lixin.basic.utils.LoginContext;
import cn.lixin.basic.utils.PageList;
import cn.lixin.user.domain.Logininfo;
import cn.lixin.user.domain.User;
import cn.lixin.user.domain.UserDto;
import cn.lixin.user.query.UserQuery;
import cn.lixin.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController //@Controller+@ResponseBody(转换json)
@RequestMapping("/user")
@Api(tags = "登录接口",description = "登录接口详细描述")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "查询所有部门")
    public List<User> queryAll(){
        return userService.loadAll();
    }

    /**
     * 根据id查询部门
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个部门",notes = "根据id查询部门")
    public User queryById(@PathVariable("id") Long id){ // PathVariable：一个{}的时候可以不写
        return userService.loadById(id);
    }

    /**
     * 高级查询
     * @return
     * RequestBody: 如果请求的是一个对象，则用RequestBody ，请求体
     *  axios get不支持传对象
     */
    @PostMapping
    @ApiOperation(value = "高级查询",notes = "根据query高级查询")
    public PageList<User> pageList(@RequestBody UserQuery query){ // PathVariable：一个{}的时候可以不写
        return userService.queryPage(query);
    }

    /**
     * 修改添加
     * PutMapping: 修改或添加
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if (user.getId() == null){
                userService.save(user);
            }else {
                userService.update(user);
            }
            System.out.println(user);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 删除
     * DeleteMapping: 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "如果有id就是修改否则就是添加")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
           userService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }


    /**
     * 删除所有
     *
     * @param ids
     * RequestBody : 数组也是对象
     */
    @PatchMapping
    @ApiOperation(value = "删除",notes = "如果有id就是修改否则就是添加")
    public AjaxResult removeList(@RequestBody List<Long> ids){
        try {
            userService.removeList(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 手机号注册
     * @param userDto
     * @return
     */
    @PostMapping("/phone/register")
    @ApiOperation(value = "手机号注册",notes = "手机号注册")
    public AjaxResult register(@RequestBody UserDto userDto){ // 将登录信息传入
        try {
            userService.register(userDto);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("注册失败!"+e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误!"+e.getMessage());
        }
    }

    /**
     * 根据id查询部门
     * @return
     */
    @PostMapping("/person")
    public User queryByLogininfoId(HttpServletRequest request){
        Logininfo currentLoginInfo = LoginContext.getCurrentLoginInfo(request);
        System.out.println(userService.queryByLogininfoId(currentLoginInfo.getId()));
        return userService.queryByLogininfoId(currentLoginInfo.getId());
    }


}
